﻿using System.IO;
using System.Web;
using System;
using DatNuocViet.App_GlobalResources;
using System.Web.Mvc;

namespace DatNuocViet.Ultility
{
    public static class Helper
    {
        #region Image
        public static bool TestImage(HttpPostedFileBase Image, int Size, bool nullReturn)
        {
            if (Image == null || Image.ContentLength == 0)
            {
                return nullReturn;
            }
            try
            {
                string extension = Path.GetExtension(Image.FileName);
                extension = extension.ToLower();
                if (Image.ContentLength <= Size && (extension == ".jpg" || extension == ".gif" || extension == ".png"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static string SaveFile(HttpPostedFileBase Image, string path, string physicalPath)
        {
            try
            {
                string temp = Guid.NewGuid().ToString();
                string extension = Path.GetExtension(Image.FileName);
                extension = extension.ToLower();
                string fileName = String.Format("{0}" + extension, Guid.NewGuid().ToString());
                string imagePath = Path.Combine(physicalPath, fileName);
                Image.SaveAs(imagePath);
                return (path + fileName);
            }
            catch (Exception)
            {
                return "~/Content/Images/no_image.gif";
            }
        }
        #endregion

        public static string Label(string name)
        {
            try
            {
                if (HttpContext.Current.Session["Language"] == null)
                {
                    HttpContext.Current.Session["Language"] = "vi";
                }

                string CultureInfo = HttpContext.Current.Session["Language"].ToString();
                string result = (CultureInfo == "vi") ? vi_VN.ResourceManager.GetString(name) : en_US.ResourceManager.GetString(name);
                return result;
            }
            catch
            {
                return "UnKnown";
            }
        }

        public static string Truncate(string input, int length)
        {
            if (input.Length <= length)
            {
                return input;
            }
            return input.Substring(0, length) + "...";
        }
    }
}